﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.DataVisualization.Charting;
using GraphDrawer.GraphProvider;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace GraphDrawer {
	public partial class PatientDetails : UserControl {
		public ObservableCollection<Pair> data = new ObservableCollection<Pair>();

		public PatientDetails() {
			InitializeComponent();

			Random a = new Random();
			data.Add(new Pair {
				Name = DateTime.Now.Day.ToString(),
				Score = a.Next(0, 6)
			});
			data.Add(new Pair {
				Name = DateTime.Now.AddDays(1).Day.ToString(),
				Score = a.Next(0, 6) + a.NextDouble()
			});
			data.Add(new Pair {
				Name = DateTime.Now.AddDays(2).Day.ToString(),
				Score = a.Next(0, 6) + a.NextDouble()
			});
			data.Add(new Pair {
				Name = DateTime.Now.AddDays(3).Day.ToString(),
				Score = a.Next(0, 6) + a.NextDouble()
			});
			data.Add(new Pair {
				Name = DateTime.Now.AddDays(4).Day.ToString(),
				Score = a.Next(0, 6) + a.NextDouble()
			});
			data.Add(new Pair {
				Name = DateTime.Now.AddDays(5).Day.ToString(),
				Score = a.Next(0, 6) + a.NextDouble()
			});

			((LineSeries)this.chartLine.Series[0]).ItemsSource = data;
		}

		private void exp1_Collapsed(object sender, RoutedEventArgs e) {
			MainGrid.Height = 58.0;
		}

		private void exp1_Expanded(object sender, RoutedEventArgs e) {
			if (MainGrid != null) {
				MainGrid.Height = 300.0;
			}
		}

		public void UpdateDetails(ObservableCollection<PairKeyValue> Keys) {
			Boolean photoExsist = false;
			String name = String.Empty;
			String surname = String.Empty;
			foreach (var description in Keys) {
				if (description.Key == "photo") {
					PatientPhoto.UriSource = new Uri(description.Value);
					photoExsist = true;
				}
				if (description.Key == "name") {
					name = description.Value;
				}
				if (description.Key == "surname") {
					surname = description.Value;
				}
				if (description.Key == "age") {
					PatientAge.Text = description.Value;
				}
				if (description.Key == "location") {
					Patientlocation.Text = description.Value;
				}
			}

			Random a = new Random((int)DateTime.Now.Ticks);

			foreach (var item in data) {
				item.Score = a.Next(0, 6) + a.NextDouble();
			}

			PatientName.Text = String.Format("{0} {1}", name, surname);

			if (!photoExsist) {
				PatientPhoto.UriSource = new Uri(Resource.NoPhotoURL);
			}
		}

	}

	public class Pair : INotifyPropertyChanged {
		private string name;

		public string Name {
			get {
				return name;
			}
			set {
				if (value != name) {
					name = value;
					onPropertyChanged(this, "Name");
				}
			}
		}

		private Double score;

		public Double Score {
			get {
				return score;
			}
			set {
				if (score != value) {
					score = value;
					onPropertyChanged(this, "Score");
				}
			}
		}

		#region  INotifyPropertyChanged Members

		public event PropertyChangedEventHandler PropertyChanged;

		private void onPropertyChanged(object sender
			, string propertyName) {

			if (this.PropertyChanged != null) {
				PropertyChanged(sender,
					 new PropertyChangedEventArgs(propertyName));
			}
		}

		#endregion
	}
}
